﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public class DockPanel : System.Windows.Forms.Panel
    {
        public DockPanel()
        {
            base.BackColor = Color.Transparent;
            if (!DesignMode) InitContentPanel();
        }


        private DockTipForm form2 = null;
        private DockTabGroup m_CenterGroup;
        public DockTabGroup CenterGroup
        {
            get { return m_CenterGroup; }
        }

        public void InitContentPanel()
        {
            this.m_CenterGroup = new DockTabGroup();
            this.m_CenterGroup.Dock = DockStyle.Fill;
            this.Controls.Add(this.m_CenterGroup);
        }


        public void ShowDLPanel()
        {
            form2 = DockTipForm.ShowDLPanel(this);
        }
        public void HideDLPanel()
        {
            try
            {
                if (form2 != null)
                {
                    form2.Close();
                    form2.Dispose();
                    form2 = null;
                }
            }
            catch (Exception) { }
        }
        public void ShowControl(Panel ctrl)
        {
            DockFloatForm.ShowControl(this, form2, ctrl);
        }
        public void DoDock(Control ctrl)
        {
            DockEventArgs arg = form2.NewDockEventArgs();
            if (arg == null) return;

            var ctrl0 = ctrl;
            var ctrl1 = arg.Control;

            MessageBox.Show(ctrl0.GetType().ToString() + " > " + ctrl1.GetType().ToString());
        }




        public DockLayoutInfo GetLayoutInfo()
        {
            return GetLayoutInfo(this);
        }
        public DockLayoutInfo GetLayoutInfo(Control parent)
        {
            if (parent == null || parent.Controls.Count <= 0) return null;
            var ctrl = parent.Controls[0];
            if (ctrl == CenterGroup)
            {
                DockLayoutInfo layout = new DockLayoutInfo(DockLayoutEnum.Content, ctrl);
                layout.Rect = GetRect(ctrl);
                return layout;
            }
            else if (ctrl is SplitContainer)
            {
                var spliter = (SplitContainer)ctrl;
                DockLayoutInfo layout = new DockLayoutInfo((spliter.Orientation == Orientation.Horizontal ? DockLayoutEnum.Horizontal : DockLayoutEnum.Vertical), ctrl);
                layout.Layout1 = GetLayoutInfo(spliter.Panel1);
                layout.Layout2 = GetLayoutInfo(spliter.Panel2);
                layout.Rect = GetRect(ctrl);
                return layout;
            }
            else if (ctrl is DockTabGroup)
            {
                DockLayoutInfo layout = new DockLayoutInfo(DockLayoutEnum.TabGroup, ctrl);
                layout.Rect = GetRect(ctrl);
                return layout;
            }

            return null;
        }
        private Rectangle GetRect(Control ctrl)
        {
            Rectangle rect1 = this.RectangleToScreen(new Rectangle(Point.Empty, this.Size));
            Rectangle rect2 = ctrl.RectangleToScreen(new Rectangle(Point.Empty, ctrl.Size));
            Rectangle rect3 = new Rectangle(rect2.X - rect1.X, rect2.Y - rect1.Y, rect2.Width, rect2.Height);
            return rect3;
        }




    }

    public class DockLayoutInfo
    {
        public DockLayoutInfo(DockLayoutEnum @enum, Control ctrl)
        {
            this.Enum = @enum;
            this.Control = ctrl;
        }

        public DockLayoutEnum Enum { get; set; }
        public Rectangle Rect { get; set; }
        public Control Control { get; set; }
        public DockLayoutInfo Layout1 { get; set; }
        public DockLayoutInfo Layout2 { get; set; }
        public Rectangle TipRect
        {
            get
            {
                Rectangle rect1 = this.Rect;
                Rectangle rect2 = new Rectangle(0, 0, 90, 90);
                if (rect1.Width < rect2.Width || rect1.Height < rect2.Height) return Rectangle.Empty;
                Rectangle rect3 = new Rectangle((rect1.X) + (rect1.Width - rect2.Width) / 2, (rect1.Y) + (rect1.Height - rect2.Height) / 2, rect2.Width, rect2.Height);
                return rect3;
            }
        }


        public bool MouseInRect(DockPanel dockPanel)
        {
            var point1 = Control.MousePosition;
            var point2 = dockPanel.PointToClient(point1);
            return Rect.Contains(point2);
        }
        public bool MouseInTipRect(DockPanel dockPanel)
        {
            var point1 = Control.MousePosition;
            var point2 = dockPanel.PointToClient(point1);
            return TipRect.Contains(point2);
        }

        public DockLayoutInfo GetContent()
        {
            if (this.Enum == DockLayoutEnum.Content) return this;

            var result1 = Layout1 == null ? null : Layout1.GetContent();
            if (result1 != null) return result1;

            var result2 = Layout1 == null ? null : Layout2.GetContent();
            if (result2 != null) return result2;

            return null;
        }
        public List<DockLayoutInfo> GetTabGroups()
        {
            if (this.Enum == DockLayoutEnum.TabGroup) return new List<DockLayoutInfo> { this };

            List<DockLayoutInfo> list = new List<DockLayoutInfo>();

            var result1 = Layout1 == null ? null : Layout1.GetTabGroups();
            if (result1 != null) list.AddRange(result1);

            var result2 = Layout1 == null ? null : Layout2.GetTabGroups();
            if (result2 != null) list.AddRange(result2);

            return list;
        }
    }

    public enum DockLayoutEnum
    {
        Content,
        TabGroup,
        Vertical,
        Horizontal,
    }

    public enum DockAlignment
    {
        Top,
        Bottom,
        Left,
        Right,
        Center,
    }

    public class DockEventArgs : EventArgs
    {
        public Control Control;
        public DockLayoutInfo TipLayout;

    }

}
